<!-- 
	全局混入 学习参考：https://cn.vuejs.org/v2/guide/mixins.html
	后面新添加：歌词解析 日期格式问题
 -->
<script>
	
	import audioUtils from '../audioUtils.js'
	
	import{
		mapState,
		mapMutations
	}from 'vuex'
	
	
	export default{
		data() {
			return{
				
			};
		},
		computed: {
			...mapState(['audioId', 'audioLrc', 'audioMeta']),
			/* 音频缓存百分比 */
			audioBuffedPerc(){
				let {buffered=0, duration=0} = this.audioMeta;
				let i = ((buffered/duration) * 100).toFixed(2);
				return i;
			},
			/* 音频播放百分比 */
			audioPlayPerc(){
				let {currentTime=0, duration=0} = this.audioMeta;
				
				let i = ((currentTime/duration) * 100).toFixed(2);
				return i;
			},
		},
		methods:{
			...mapMutations({
				setAudioId: 'setAudioId',
				setAudioTitle: 'setAudioTitle',
				setPicUrl: 'setPicUrl',
				setAudioLrc: 'setAudioLrc',
				setAudioMeta: 'setAudioMeta'
			}),
			/* 
				播放音乐 打开播放页面
			 */
			// playMusic(params={}){
			playMusic(params){
				let { id, picUrl, name} = params
				let { audioId, audioMeta } = this
				if( id != audioId || audioMeta.paused ){
					// this.setAudioId(id);
					this.setAudioTitle(name);
					this.setPicUrl(picUrl);
				}
				
				uni.navigateTo({
					url: '/pages/playMusic/playMusic?id=' + id,
				});
				
			},
			
			
			/* 
				转换数字显示（单位:亿/万）
				var: 数值
				fixedNum 保留小数点
				ch 是否为中文
			 */
			formatNumber(val, fixedNum=2, ch=true){
				if(!val){
					return ";"
				}
				if(val >= 10000000){
					if(fixedNum < 4){
						fixedNum = 4
					}
					return (val/100000000).toFixed(fixedNum) + (ch ? '亿' : "ww");
				}
				if(val >= 10000){
					return (val/10000).toFixed(fixedNum) + (ch ? '万': 'w');
				}
				return val;
			},
			
			/* 时间转换  分钟 */
			parseMinute(num=0) {
				num < 0 ? 0 : num
				let i = parseInt(num/60)
				return i < 10 ? '0'+ i : i
			},
			
			/* 时间转换 秒 */
			parseSecond(num = 0) {
				let i = parseInt(num % 60)
				return i < 10 ? '0'+ i: i
			},
			
			/* 根据id播放音乐 */
			playMusicById(paramId) {
				let {audioId, audioMeta} = this
				if(paramId == audioId && audioMeta.paused == false){
					return
				}
				this.setAudioId(paramId)
				
				// 获取音乐 url
				this.$api.songUrl({data:{id: paramId}})
				.then(res => {
					let songs = res.data || []
					if(songs.length > 0 && songs[0].url ){
						// 播放音乐
						this.playMusicByUrl(songs[0].url);
					}else {
						uni.showModal({
							title: "失败",
							content: "无法获取该歌曲的播放地址，应该是没版权~"
						})
					}
					
					//获取歌词
					return this.$api.lyric({data: {id: paramId}})
					.then(data => {
						//创建歌词对象
						let audioLrc = audioUtils.createLrcObj(data.lrc.lyric);
						this.setAudioLrc(audioLrc);
					})
				})
			},
			/* 根据url播放音乐 */
			playMusicByUrl(url) {
				let audioCtx = getApp().globalData.audioCtx
				console.log("132312132132132")
				console.log(audioCtx)
				console.log(getApp())
				
				if(audioCtx){
					audioCtx.src = url
					audioCtx.play()
				}
				return audioCtx
				
			},
			
			
			/* 播放音乐 */
			doPlayAudio(){
				let audioCtx = getApp().globalData.audioCtx;
				console.log("播放音乐")
				console.log(audioCtx)
				if (audioCtx != null) {
					audioCtx.play();
				}
			},

			/* 暂停音乐 */
			doPauseAudio(){
				let audioCtx = getApp().globalData.audioCtx;
				if (audioCtx != null) {
					audioCtx.pause();
				}
			},
			
			toFixed(num, fNum=2){
				return num.toFixed(fNum);
			},
		}
		
	}
	
</script>
